Basic Programming Principles in TorqueScript
But what if you don’t have a programming background, and aren’t familiar with basic principles of software implementation? This article will provide an overview of some of the basic programming principles, and give examples of how they are accomplished within TorqueScript.
Data Storage
Variables
Variable ScopeIn general programming, we want to maintain flexibility and allow humans to read our code and understand what’s going on without too much complexity—and sometimes that means being able to use the same name for a variable in different places in our code. The problem is that since our work may be most readable if we use the same variable names in many different places, yet we do not want the same values in many different places, we seem to be stuck—either we keep track of large numbers of variable names like index1, index2, index3, index4, or we have to share data when it wouldn’t be appropriate. This brings up the general concept of scope, which is loosely defined as a way for data to only be available at a specific place in our code. In the case of variables, we have two different types of scope:
So, how do we know what a specific scope is? The concept is a bit more complex than this basic description, but in general, a scope is defined as the section of code beginning where you start using the variable, and the next sequential “end” of a statement body. We’ll get to program flow later on and define statement body a bit more accurately, but for now you can consider the end of a statement body as the next } sign within the code. Object Data StorageWe have an additional way to store data that is specific to objects within our engine (if you need a quick primer on how Objects work within Torque, you can take a quick look at this short article: INTRO TO OOP). Every object within our engine can store data within a special type of variable called a field that only applies to that specific object—changing the value of a field on one object does not affect any other objects, even when they have the same field defined. Fields can be considered very similar to locally scoped variables, but instead of being scoped to a specific section of our program’s code, they are scoped instead to the object itself—if a section of code has access to an object, it has access to the object’s fields as well. There are two types of fields for objects:
Program Execution Flow
As part of the TorqueScript language definition and syntax, we use a very common technique for creating logical separation within our code called a statement body. A statement body is simply one or more TorqueScript statements (valid commands or expressions), optionally surrounded by the { (statement body BEGIN) and } (statement body END) characters if we want to group more than one statement together. In many cases however, we only want a particular statement body to execute under certain conditions – for example, a particular variable has a certain value. This type of capability is called conditional execution, and the specific example given would be written as an “if” statement within our program, also known as conditional branching. If (%value == 5) { echo("The value was 5!"); } The “if” statement is a part of TorqueScript’s execution flow control capabilities along with several other types of control techniques:
Engine vs. Script
TorqueScript allows us to interact with the engine as it us running in a few different ways—our TorqueScript program itself runs based on direction from the engine, we can execute Console Method calls as described above, and finally we can respond to events that occur within the engine itself. While our game is running, there are thousands of calculations and operations a second occurring beneath or notice—but sometimes it’s important for our TorqueScript program to be notified when something important occurs—such as a collision between two objects, when a level is completed loading, or even simply when an object is removed from the game. These types of occurrences are called “events”, and Torque provides us with the ability to respond to events with the concept of Callbacks and Callback Handlers. Callbacks are simply a notification to TorqueScript that a particular event has occurred, and provides information (designated by the C++ programmer responsible for that portion of the C++ engine code) about the event to TorqueScript. Callback Handlers are simply the section of TorqueScript code that we want to execute when a particular Callback occurs—for example, if one of our objects collides with another object, we may want to increase the score for the player, or possibly cause the player object to “die” and re-spawn. There are hundreds of callbacks provided by the engine, and we are able to implement any TorqueScript commands and statements we wish in response to those events. Summary
|